param(
    [switch]$Clean,
    [switch]$Test,
    [switch]$Benchmark,
    [switch]$Release,
    [switch]$Help,
    [switch]$Interactive,
    [switch]$All
)

function Write-Info { param([string]$Message) Write-Host "[INFO] $Message" -ForegroundColor Green }
function Write-Warning { param([string]$Message) Write-Host "[WARNING] $Message" -ForegroundColor Yellow }
function Write-Error { param([string]$Message) Write-Host "[ERROR] $Message" -ForegroundColor Red }
function Write-Success { param([string]$Message) Write-Host "[SUCCESS] $Message" -ForegroundColor Green }

function Test-RustEnvironment {
    try {
        $rustVersion = rustc --version
        Write-Info "Rust environment OK: $rustVersion"
        return $true
    } catch {
        Write-Error "Rust not installed, visit https://rustup.rs/"
        return $false
    }
}

function Clear-BuildFiles {
    Write-Info "Cleaning build files..."
    if (Test-Path "target") { Remove-Item -Recurse -Force "target" }
    Set-Location core; cargo clean; Set-Location ..
    Write-Success "Clean completed"
}

function Build-Project {
    Write-Info "Building project..."
    $process = Start-Process -FilePath "cargo" -ArgumentList "build", "--release" -NoNewWindow -Wait -PassThru
    if ($process.ExitCode -ne 0) { Write-Error "Build failed"; return $false }
    Write-Success "Build completed"; return $true
}

function Invoke-TestSuite {
    Write-Info "Running tests..."
    if (Test-Path "target\release\pipit.exe") {
        $process = Start-Process -FilePath "target\release\pipit.exe" -ArgumentList "--test" -NoNewWindow -Wait -PassThru
        if ($process.ExitCode -eq 0) { Write-Success "Tests passed"; return $true }
        else { Write-Error "Tests failed"; return $false }
    } else { Write-Error "Executable not found"; return $false }
}

function Invoke-Benchmark {
    Write-Info "Running benchmarks..."
    if (Test-Path "target\release\pipit.exe") {
        $process = Start-Process -FilePath "target\release\pipit.exe" -ArgumentList "--benchmark" -NoNewWindow -Wait -PassThru
        if ($process.ExitCode -eq 0) { Write-Success "Benchmark completed"; return $true }
        else { Write-Error "Benchmark failed"; return $false }
    } else { Write-Error "Executable not found"; return $false }
}

function New-ReleasePackage {
    Write-Info "Creating release package..."
    $releaseDir = "release"
    if (-not (Test-Path $releaseDir)) { New-Item -ItemType Directory -Path $releaseDir | Out-Null }
    if (Test-Path "target\release\pipit.exe") {
        Copy-Item "target\release\pipit.exe" "$releaseDir\pipit-v1.0.0-windows.exe"
        Copy-Item "examples\*.pi" "$releaseDir\" -Recurse -ErrorAction SilentlyContinue
        Write-Success "Release package created: $(Get-Location)\$releaseDir"
        return $true
    } else { Write-Error "Executable not found"; return $false }
}

function Show-Help {
    Write-Host "Pipit Language v1.0.0 Build Script"
    Write-Host "=================================="
    Write-Host "Usage: .\build.ps1 [options]"
    Write-Host "Options:"
    Write-Host "  -Clean       Clean build files"
    Write-Host "  -Test        Run test suite"
    Write-Host "  -Benchmark   Run performance benchmarks"
    Write-Host "  -Release     Create release package"
    Write-Host "  -Interactive Interactive mode"
    Write-Host "  -All         Full build process"
    Write-Host "  -Help        Show this help"
}

function Start-Interactive {
    Write-Host "Pipit Interactive REPL Mode"
    Write-Host "Type 'exit' to quit, 'help' for help"
    while ($true) {
        Write-Host "pipit> " -ForegroundColor Yellow -NoNewline
        $input = Read-Host
        if ($input -eq "exit") { break }
        if ($input -eq "help") { Write-Host "Commands: help, exit"; continue }
        if ($input -eq "version") {
            if (Test-Path "target\release\pipit.exe") { & "target\release\pipit.exe" --version }
            else { Write-Error "Please build first" }
            continue
        }
    }
    Write-Host "Goodbye!"
}

if ($Help) { Show-Help; exit 0 }

Write-Host "Pipit Language v1.0.0 Build Script" -ForegroundColor Cyan
Write-Host "=================================="

if (-not (Test-RustEnvironment)) { exit 1 }

if ($All) { $Clean = $true; $Test = $true; $Release = $true }

if ($Clean) { Clear-BuildFiles }
if (-not (Build-Project)) { exit 1 }
if ($Test -and -not (Invoke-TestSuite)) { exit 1 }
if ($Benchmark -and -not (Invoke-Benchmark)) { exit 1 }
if ($Release -and -not (New-ReleasePackage)) { exit 1 }
if ($Interactive) { Start-Interactive }

Write-Host "Build completed!" -ForegroundColor Green